package test9;

import java.util.Arrays;
import java.util.Comparator;
//Comparator接口
class Student {
    public int age;
    public String name;
    public double score;

    public Student(int age, String name, double score) {
        this.age = age;
        this.name = name;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", score=" + score +
                '}';
    }
}

class AgeComparator implements Comparator<Student>{
    @Override
    public int compare(Student o1, Student o2) {
        return o1.age-o2.age;
    }
}

class ScoreComparator implements Comparator<Student>{
    @Override
    public int compare(Student o1, Student o2) {
        return (int)(o1.score-o2.score);
    }
}

public class Demo9 {

    public static void main2(String[] args) {
        Student student1=new Student(12,"aaa",98.9);
       Student student2=new Student(2,"abc",89.9);
       AgeComparator ageComparator=new AgeComparator();
        System.out.println(ageComparator.compare(student1,student2));
    }

    public static void main(String[] args) {
        Student[] students=new Student[3];
        students[0]=new Student(12,"aaa",98.9);
        students[1]=new Student(62,"abc",89.9);
        students[2]=new Student(18,"zhangsan",18.9);
        System.out.println(Arrays.toString(students));
        AgeComparator ageComparator=new AgeComparator();
        ScoreComparator scoreComparator=new ScoreComparator();
        Arrays.sort(students,scoreComparator);
        System.out.println(Arrays.toString(students));
    }
}
